package org.crazyit.activiti_d3._6_UserQuery;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.User;
import org.crazyit.activiti_d3._3_UpdateUser.UpdateUser;

import java.util.List;
import java.util.UUID;

public class UserQuery {

    public static void main(String[] args) {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到身份服务组件实例
        IdentityService identityService = engine.getIdentityService();

        //3. 创建两个用户
        String id1 = UUID.randomUUID().toString();
        String id2 = UUID.randomUUID().toString();
        UpdateUser.createUser(identityService,id1,"angus","young","yangenxiong@163.com","abc");
        UpdateUser.createUser(identityService,id2,"angus2","young2","abc@163.com","123");

        //4. 调用UserQuery 的各个查询方法
        //userId
        User user1 = identityService.createUserQuery().userId(id1).singleResult();
        System.out.println("userId:" + user1.getFirstName());

        //userFirstName
        User user2 = identityService.createUserQuery().userFirstName("angus").list().get(0);
        System.out.println("userFirstName:" + user2.getFirstName());

        //userFirstNameLike
        List<User> list1 = identityService.createUserQuery().userFirstNameLike("angus%").list();
        System.out.println("userFirstNameLike:" + list1.size());

        //userLastName
        User user3 = identityService.createUserQuery().userLastName("young").list().get(0);
        System.out.println("userLastName:" + user3.getLastName());

        //userLastNameLike
        List<User> list2 = identityService.createUserQuery().userFirstNameLike("young%").list();
        System.out.println("userLastNameLike:" + list2.size());

        //userEmail
        User user4 = identityService.createUserQuery().userEmail("abc@163.com").list().get(0);
        System.out.println("userEmail:" + user4.getEmail());

        //userEmailLike
        List<User> list3 = identityService.createUserQuery().userEmailLike("%163.com").list();
        System.out.println("userEmailLike:" + list3.size());

        //使用NativeQuery
        List<User> list4 = identityService.createNativeUserQuery().sql("select * from ACT_ID_USER where EMAIL_ = #{email}").parameter("email", "yangenxiong@163.com").list();
        System.out.println("native query:" + list4.get(0).getEmail());

    }

}
